var KOC = require("koc.common.utils");

var Config = require("../../config");

var StateData = require("../statedata");
var FileBuilder = require("../../lib/file-builder");

module.exports = {
    ////////////////////////////////////////////
    //文件上传服务
    ////////////////////////////////////////////
    FileUpload: function (req, res) {
        if (!req.file) {
            res.jsonp({flag: 1});
            return;
        }
        var _FileName = req.file.path.substring(req.file.path.lastIndexOf((req.file.path.indexOf("\\") >= 0 ? "\\" : "/")) + 1);
        var _Ext = req.file.originalname.substring(req.file.originalname.lastIndexOf(".")).replace(".", "").toLowerCase();
        var _ExtSafe = ["jpg", "jpge", "png", "gif", "xml", "htm", "html", "css", "rar", "zip", "xls", "xlsx", "txt"];
        if (!KOC.Lodash.includes(_ExtSafe, _Ext)) {
            res.jsonp({flag: 2});
            return;
        }
        res.jsonp({
            flag: 0,
            FileName: _FileName + "." + _Ext,
            Url: Config.Common.Virtual_Path.File_Common_Temp + _FileName
        });
    },
    ////////////////////////////////////////////
    //图片服务
    ////////////////////////////////////////////
    Image: {
        Temp: function (req, res) {
            var parm = {
                FileName: KOC.StringUtils.ToString(req.params.filename)
            };
            parm.File = FileBuilder.Common.TempFilePath(parm.FileName);
            parm.FileDefault = "";
            KOC.Async.series([
                //判断图片
                function (callback) {
                    if (!parm.File) {
                        callback();
                        return;
                    }
                    KOC.FSExtra.exists(parm.File, function (fileExists) {
                        if (fileExists) {
                            parm.Data = parm.File;
                        }
                        callback();
                    });
                },
                //判断默认图片
                function (callback) {
                    if (parm.Data || !parm.FileDefault) {
                        callback();
                        return;
                    }
                    KOC.FSExtra.exists(parm.FileDefault, function (fileExists) {
                        if (fileExists) {
                            parm.Data = parm.FileDefault;
                        }
                        callback();
                    });
                }
            ], function () {
                if (!parm.Data) {
                    res.jsonp({flag: 2});
                    return;
                }
                res.sendFile(parm.Data);
            });
        },
        User: function (req, res) {
            var parm = {
                Type: req.params.type,
                ID: KOC.StringUtils.ToInt(req.params.id, -1),
                Size: KOC.StringUtils.ToString(req.params.size).split("."),
                Size_Width: -1,
                Size_Height: -1
            };
            if (parm.Size.length == 2) {
                parm.Size_Width = KOC.StringUtils.ToIntPositive(parm.Size[0], -1);
                parm.Size_Height = KOC.StringUtils.ToIntPositive(parm.Size[1], -1);
            }
            switch (parm.Type) {
                case "avatar":
                    parm.File = FileBuilder.User.Avatar.Path(parm.ID, parm.Size_Width, parm.Size_Height);
                    parm.FileDefault = Config.Common.Physical_Path.Static + "common/image/user-avatar" + (parm.ID > 0 ? ("-" + (parm.ID % 10)) : "") + ".png";
                    break;
                case "cover":
                    parm.File = FileBuilder.User.Cover.Path(parm.ID, parm.Size_Width, parm.Size_Height);
                    parm.FileDefault = Config.Common.Physical_Path.Static + "common/image/user-cover.png";
                    break;
                case "idcard-a":
                case "idcard-b":
                    //region 判断权限
                    if (StateData.Value(req, res).User_ID !== parm.ID
                        && StateData.RightCode.Admin(req, res, "{User}[Base](Info-IDCard)|{User}[Anchor](Apply-Edit)|{User}[Family](Apply-Edit)") !== 0) {
                        res.jsonp({flag: -1});
                        return;
                    }
                    //endregion
                    parm.File = FileBuilder.User.IDCard.Path(parm.ID, parm.Type.replace("idcard-", ""));
                    parm.FileDefault = "";
                    break;
                default:
                    res.jsonp({flag: 1});
                    return;
            }
            KOC.Async.series([
                //判断图片
                function (callback) {
                    if (!parm.File) {
                        callback();
                        return;
                    }
                    KOC.FSExtra.exists(parm.File, function (fileExists) {
                        if (fileExists) {
                            parm.Data = parm.File;
                        }
                        callback();
                    });
                },
                //判断默认图片
                function (callback) {
                    if (parm.Data || !parm.FileDefault) {
                        callback();
                        return;
                    }
                    KOC.FSExtra.exists(parm.FileDefault, function (fileExists) {
                        if (fileExists) {
                            parm.Data = parm.FileDefault;
                        }
                        callback();
                    });
                }
            ], function () {
                if (!parm.Data) {
                    res.jsonp({flag: 2});
                    return;
                }
                res.sendFile(parm.Data);
            });
        },
        Report: function (req, res) {
            var parm = {
                ID: KOC.StringUtils.ToInt(req.params.id, -1),
                Name: req.params.name
            };
            if (parm.ID <= 0) {
                res.jsonp({flag: 1});
                return;
            }
            if (!parm.Name) {
                res.jsonp({flag: 2});
                return;
            }
            parm.File = FileBuilder.UserReport.Report.Path(parm.ID, parm.Name);
            KOC.FSExtra.exists(parm.File, function (fileExists) {
                if (fileExists) {
                    res.sendFile(parm.File);
                    return;
                }
                res.jsonp({flag: 3});
            });
        },
        Event: function (req, res) {
            var parm = {
                ID: KOC.StringUtils.ToInt(req.params.id, -1),
                Name: req.params.name
            };
            if (parm.ID <= 0) {
                res.jsonp({flag: 1});
                return;
            }
            if (!parm.Name) {
                res.jsonp({flag: 2});
                return;
            }
            parm.File = FileBuilder.UserEvent.Event.Path(parm.ID, parm.Name);
            KOC.FSExtra.exists(parm.File, function (fileExists) {
                if (fileExists) {
                    res.sendFile(parm.File);
                    return;
                }
                res.jsonp({flag: 3});
            });
        }
    },
    ////////////////////////////////////////////
    //下载服务
    ////////////////////////////////////////////
    Download: {
        App: function (req, res) {
            res.locals.Title = "全民秀直播";
            var _UserAgent = req.headers["user-agent"].toLowerCase();
            var _UserAgent_Weixin = _UserAgent.match(/micromessenger/i) == "micromessenger";
            var _APPData = {
                // Android: "http://a.app.qq.com/o/simple.jsp?pkgname=com.carneting",
                // Android_Weixin: "http://a.app.qq.com/o/simple.jsp?pkgname=com.carneting",
                // iPhone: "https://itunes.apple.com/cn/app/ka-nai-ding-zi-zhu-yang-che/id957667046",
                // iPhone_Weixin: "http://a.app.qq.com/o/simple.jsp?pkgname=com.carneting",
                // iPad: "https://itunes.apple.com/cn/app/ka-nai-ding-zi-zhu-yang-che/id957667046",
                // iPad_Weixin: "http://a.app.qq.com/o/simple.jsp?pkgname=com.carneting",
                // Default: "http://a.app.qq.com/o/simple.jsp?pkgname=com.carneting"

                Android: "http://a.app.qq.com/o/simple.jsp?pkgname=com.tiantian.quanminxiu",
                Android_Weixin: "http://a.app.qq.com/o/simple.jsp?pkgname=com.tiantian.quanminxiu",
                iPhone: "https://itunes.apple.com/cn/app/xi-you-zhi-bo/id1111125887",
                iPhone_Weixin: "https://itunes.apple.com/cn/app/xi-you-zhi-bo/id1111125887",
                iPad: "https://itunes.apple.com/cn/app/xi-you-zhi-bo/id1111125887",
                iPad_Weixin: "https://itunes.apple.com/cn/app/xi-you-zhi-bo/id1111125887",
                Default: "http://a.app.qq.com/o/simple.jsp?pkgname=com.tiantian.quanminxiu"
            };
            //指定跳转
            switch (req.params.type) {
                case "ios":
                    res.redirect(_APPData.iPhone);
                    return;
                case "android":
                    res.redirect(_APPData.Android);
                    return;
            }
            //Android
            if (_UserAgent.match(/android/i) == "android") {
                res.redirect(_UserAgent_Weixin ? _APPData.Android_Weixin : _APPData.Android);
                return;
            }
            //iPhone
            if (_UserAgent.match(/iphone/i) == "iphone") {
                res.redirect(_UserAgent_Weixin ? _APPData.iPhone_Weixin : _APPData.iPhone);
                return;
            }
            //iPad
            if (_UserAgent.match(/ipad/i) == "ipad") {
                res.redirect(_UserAgent_Weixin ? _APPData.iPad_Weixin : _APPData.iPad);
                return;
            }
            //默认跳转
            res.redirect(_APPData.Default);
        }
    }
};